local ObjectPoolManager = class("ObjectPoolManager")
local ObjectPool = require("manager.ObjectPool")
local ResPool = require("manager.ResPool")
local GameUtils = require("utils.GameUtils")

function ObjectPoolManager:ctor()
    self._objectPoolMap = {}
    self._resPoolMap = {}
    local gameObject = GameUtils.InstantiateEmptyObject("ObjectPoolManager")
    local behaviour = gameObject:AddComponent(typeof(CS.LuaBehaviour))
    behaviour:SetScriptEnv(self)
    CS.UnityEngine.GameObject.DontDestroyOnLoad(gameObject)
end

function ObjectPoolManager:addObjetcPool(key, template)
    local objectPool = self._objectPoolMap[key]
    if not objectPool then
        objectPool = ObjectPool.new(template, self, key)
        self._objectPoolMap[key] = objectPool
    end
    return objectPool
end

function ObjectPoolManager:getObject(key, template, ...)
    local pool = self:addObjetcPool(key, template)
    return pool:getObject(...)
end

function ObjectPoolManager:addResObjectPool(resPath)
    local pool = self._resPoolMap[resPath]
    if not pool then
        pool = ResPool.new(resPath, self, resPath)
        self._resPoolMap[resPath] = pool
    end
    return pool
end

function ObjectPoolManager:removeObjectPool(key)
    self._objectPoolMap[key] = nil
end

function ObjectPoolManager:removeResPool(key)
    self._resPoolMap[key] = nil
end

function ObjectPoolManager:clearObjetcPool()
    for _, pool in pairs(self._objectPoolMap) do
        pool:destroy(true)
    end
    self._objectPoolMap = {}
    for _, pool in pairs(self._resPoolMap) do
        pool:destroy(true)
    end
    self._resPoolMap = {}
end

return ObjectPoolManager
